package com.masterclient.app.businessmodule.homemodule.view.adapter.recommand;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * Created by yongqianggeng on 2018/10/6.
 */

public class HomePageWrapContentHeightViewPager extends ViewPager {

    private boolean isCanScroll = false;
    private int currentIndex;
    private int height = 0;
/**
 * 保存position与对于的View
 */
    private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<>();
    public HomePageWrapContentHeightViewPager(Context context) {
        super(context);
        setScanScroll(false);
    }


    public HomePageWrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScanScroll(false);
        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mChildrenViews != null && mChildrenViews.size() > 0) {
            height = 0;
            int currentItem = getCurrentItem();
            if (mChildrenViews.size() > 0) {
                View child = mChildrenViews.get(currentItem);
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                if (h > height) height = h;
            } else {
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(currentItem);
                    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();
                    if (h > height) height = h;
                }
            }
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 重新设置高度
     *
     * @param current
     */
    public void resetHeight(int current) {
        currentIndex = current;
        if (mChildrenViews.size() > current) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
            } else {
                layoutParams.height = height;
            }
            setLayoutParams(layoutParams);
        }
    }

    public void setpositionAndView(int position,View view) {
        if (mChildrenViews != null && view != null) {
            mChildrenViews.put(position,view);
        }
    }

    /**
     * 设置其是否能滑动换页
     * @param isCanScroll false 不能换页， true 可以滑动换页
     */
    public void setScanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return isCanScroll && super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return isCanScroll && super.onTouchEvent(ev);

    }


}
